package com.llp.declaresystem.controller;

import com.llp.declaresystem.domain.Result;
import com.llp.declaresystem.domain.entity.Unit;
import com.llp.declaresystem.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/unitmessage")
@Slf4j
public class UnitMessageController {
    @Autowired
    private UnitService unitService;

    /**
     * 展示单位信息
     * @return
     */
    @GetMapping
    public Result getAll(){
        List<Unit> units = unitService.list();
        if(units != null){
            return new Result<>(200,"返回单位信息",units);
        }else {
            return new Result<>(400,"返回单位信息错误",null);
        }
    }

    /**
     * 修改单位信息
     * @param unit
     * @return
     */
    @PostMapping
    public Result updateUnit(@RequestBody Unit unit){
        boolean b = unitService.saveOrUpdate(unit);
        if (b) return new Result<>(200, "修改成功", null);
        else return new Result<>(400, "修改失败", null);
    }

    /**
     * 根据unitid删除单位信息
     * @param unitid
     * @return
     */
    @DeleteMapping
    public Result deleteProject(@RequestParam("unitid")Integer unitid)
    {
        log.info("unitid={}",unitid);
        boolean b= unitService.deleteUnitById(unitid);
        if(b) return new Result(200,"删除成功",null);
        else return new Result<>(400,"出错了",null);
    }
}
